package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.mall.entity.admin.MallProductRecommendEntity;
import net.lahuobao.mall.entity.admin.MallProductRecommendHistoryEntity;
import net.lahuobao.mall.service.admin.MallProductRecommendHistoryServiceI;
import net.lahuobao.mall.service.admin.MallProductRecommendServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("mallProductRecommendHistoryService")
@Transactional
public class MallProductRecommendHistoryServiceImpl extends CommonServiceImpl implements MallProductRecommendHistoryServiceI {

    @Autowired
    private MallProductRecommendServiceI mallProductRecommendServiceI;


    @Override
    public void back(Integer id) throws Exception{

        //推荐表中有重复纪录--将推荐表中重复纪录插入到历史表中，在删除推荐表中的数据
        String sql = "SELECT mpr.* FROM lahuobao.mall_product_recommend mpr LEFT JOIN lahuobao.mall_product_recommend_history mprh ON mpr.sort = mprh.sort AND mpr.recommendType = mprh.recommendtype WHERE mprh.id = " +id;
        List<MallProductRecommendEntity> list = super.findListbySql(sql, MallProductRecommendEntity.class);
        if(list.size() > 0){
            for(MallProductRecommendEntity entity : list){
                MallProductRecommendHistoryEntity historyEntity = new MallProductRecommendHistoryEntity();
                MyBeanUtils.copyBean2Bean(historyEntity, entity);
                super.save(historyEntity);
                mallProductRecommendServiceI.delete(entity);
            }
        }

        MallProductRecommendHistoryEntity historyEntity = get(MallProductRecommendHistoryEntity.class, id);
        MallProductRecommendEntity recommendEntity = new MallProductRecommendEntity();
        MyBeanUtils.copyBean2Bean(recommendEntity, historyEntity);
        //保存在推荐表中
        mallProductRecommendServiceI.save(recommendEntity);
        //删除历史推荐表中的数据
        super.delete(historyEntity);
    }
}